
/**
* @file			LCD.c
* @author  		Eranga Perera
* @date    		December 06, 2014
* @brief   		HD44780LIB.c Compatible module
*/

/* Includes ------------------------------------------------------------------*/
#include "HD44780LIB.h"
#include "LCD.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/

static uint32_t msTick = 0;

/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

// time based interrupt handler
void SysTick_Handler()
{
	//Control the Back light
	H_LEDPWM();
	
	// Execute charge pump handler
	H_ChargePump();
	msTick++;
}



/**
* @brief	Pauses the program for the amount of time (in miliseconds) specified as parameter. 
* @param	ms		the number of milliseconds to pause
* @retval	None
*/
void delay(uint32_t ms)
{
	uint32_t start = msTick;
	while ((msTick - start) < ms);
}



/**
* @brief	LCD_Init() function will only run once, Initializes the hardware
* @param	None
* @retval	None
*/

void LCD_Init(){

//initializes LCD
H_HWInit();

// clear display
ClrDisp();	

}




/**
* @brief	The LCD_ALARM_ON() function will only run once, requests to enter code
* @param	None
* @retval	string length
*/

int LCD_ALARM_ON(){
	
	//define local variables
	const char* C;
	const char* F;
	
	C = "Alarm On";
	
	F="Enter Code";

	// mention that alar has gone off
	PStr(C, 0, 1);
	
	delay(1000);
	// clear the display
	ClrDisp();
	
	delay(1000);
	
	// Ask to enter the code
	PStr(F, 0, 1);
	
	delay(1000);
	
	//return string length
	return Strlen(F);
}



/**
* @brief	The Reset_pressed() function will only run once, requests to enter old password
* @param	None
* @retval	0
*/

int Reset_pressed(){
	
	//define local variable
	const char* S;
	S = "Old Password:";
	
	//Clear Display
	ClrDisp();
	
	// Ask to type old password
	PStr(S,0,1);
	
	
	delay (2000);
	
	// clear Display
	
	ClrDisp();
	//
	return 0;
	
}



/**
* @brief	The Enter_new_password() function will only run once, requests to enter new password
* @param	None
* @retval	0
*/

int Enter_new_password(){
	
	//define local variable
	const char* N;
	N = "New Password:";
	
	//Clear Display
	ClrDisp();
	
	// Ask to type old password
	PStr(N,0,1);
	
	
	delay (2000);
	
	// clear Display
	
	ClrDisp();
	
	// return 0
	return 0;
	
}



/**
* @brief	The wrong_code() function will only run once, indicate error
* @param	None
* @retval	0
*/

int wrong_code(){
	
	//define local variable
	const char* W;
	W = "Error";
	
	//Clear Display
	ClrDisp();
	
	// Ask to type old password
	PStr(W,0,1);
	
	
	delay (2000);
	
	// clear Display
	
	ClrDisp();
	
	//return 0
	
	return 0;
}



/**
* @brief	The LCD_write(uint8_t Data_in, int word_length) function will only run once, and it write the digits on LCD with respect to the position of the String
* @param	Data (int) , Length of the String (Mostly the methods which print out statements, the word count of these statement will be considered)
* @retval	None
*/

void LCD_write(int32_t Data_in){
	
	//clear display
	ClrDisp();
	//delay
	delay(1000);
	// display the code
	
	
	PNum(Data_in, 0, 1, 0);
		
	
	delay(1000);
}
	


